package com.example.controller;
import com.example.constant.ServicePrefixConstant;
import com.example.dto.*;
import com.example.entity.BusinessUserEntity;
import com.example.entity.SysUserEntity;
import com.example.feign.UsernameFeignClient;
import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.security.UserDetail;
import com.example.service.Oauth2RegisteredClientService;
import com.example.service.SysUserService;
import com.example.utils.ConvertUtils;
import com.example.utils.JSON;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Tag(name = "username获取用户权限")
@RestController
public class UsernameController implements UsernameFeignClient {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private Oauth2RegisteredClientService oauth2RegisteredClientService;


    @GetMapping("username/{username}")
    @Operation(summary = "username获取用户权限")
    public Result<UserDetail> getByUsername(@PathVariable("username") String username){
        try {
            UserDetail userDetail = sysUserService.getByUsername(username);
            return new Result<UserDetail>().ok(userDetail);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/username/sysusername/{username}")
    @Operation(summary = "username获取用户权限")
    public Result<SysUserDTO> getSysUsername(@PathVariable("username") String username){
        SysUserEntity sysUsername = sysUserService.getSysUsername(username);
        return new Result<SysUserDTO>().ok(ConvertUtils.sourceToTarget(sysUsername, SysUserDTO.class));
    }

    @GetMapping("/businessusername/{username}")
    @Operation(summary = "username获取用户权限")
    public Result<UserDetail> getByBusinessUsername(@PathVariable("username") String username){
        try {
            UserDetail userDetail = sysUserService.getByBusinessUsername(username);
            return new Result<UserDetail>().ok(userDetail);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/businessusername/isBusiness/{id}")
    public Result<Integer> getIsBusiness(@PathVariable("id") String id){
        Integer isBusinessUser = oauth2RegisteredClientService.getIsBusinessUser(id);
        return new Result<Integer>().ok(isBusinessUser);
    }

    @GetMapping("/businessusername/exist/{openid}")
    public Result<BusinessUserDTO> businessUsernameExist(@PathVariable("openid") String openid){
        BusinessUserEntity businessUserEntity = sysUserService.businessUsernameExist(openid);
        return new Result<BusinessUserDTO>().ok(ConvertUtils.sourceToTarget(businessUserEntity, BusinessUserDTO.class));
    }

    @PostMapping("/businessusername/create")
    public Result<String> createBusinessUser(@RequestBody BusinessUserDTO businessUserDTO){
        sysUserService.createBusinessUser(businessUserDTO);
        return new Result<String>().ok("ok");
    }

    @PostMapping("username")
    @Operation(summary = "创建超级用户管理员")
    public Result adminUsername(@RequestBody SysUserDTO dto){
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        sysUserService.save(dto);
        return Result.success();
    }

    @PostMapping("/username/thirdOauth2username")
    @Operation(summary = "创建第三方用户")
    public Result saveThirdOauth2(@RequestBody SysUserDTO dto){
//        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        sysUserService.saveThirdOauth2(dto);
        return Result.success();
    }


}